import api from '@/api/index'
import { ElMessage } from 'element-plus'
export function submitDataHanle(methods, emit, data,type) {
    if (data.length==0) return ElMessage.error('请先选择')
    api.writeForm.writeFormPerssionSettingUpdate(data,type).then(res => {
        ElMessage.success('提交成功')
        methods.handleClose()
        emit('on-update')
    })

}
export function updateData([obj, text], selectList, state) {
    const { companies, members, positions } = obj
    console.log(companies, members, positions)
    const companyList = dataPackage(companies, 'code', 'organization', '组织', selectList) // 公司
    const membersList = dataPackage(members, 'email', 'member', '成员', selectList)  //人员
    const workList = dataPackage(positions, 'code', 'role', '角色', selectList) //岗位
    let data = [...companyList, ...workList, ...membersList]
    state.submitData = data

}
// 数据组装
export function dataPackage(list, feild, filterType, filterTypeName, selectList) {
    let data = []
    if (list && list.length > 0) {
        selectList.forEach(itemPar => {
            list.forEach(item => {
                data.push({
                    filterCode: item[feild],
                    filterName: item.name,
                    filterType,
                    filterTypeName,
                    writeFormCode: itemPar.formCode,
                    writeFormId: itemPar.id,
                    writeFormName: itemPar.formName,
                    filterId: item.curId
                })
            })
        })
    }
    return data
}

export function queryPermisson(ids, curList) {
    return new Promise((resolve) => {
        let sourceList = curList.map(item => {
            return {
                formCode: item.formCode,
                tags: []
            }
        })
        api.writeForm.writeFormQuery(ids).then((res) => {
            //组装成二维位数组-先按照表单分类
            sourceList.forEach(item => {
                res.forEach(itemTwo => {
                    if (item.formCode == itemTwo.writeFormCode) {
                        item.tags.push(itemTwo)
                    }
                })
            })
            resolve(sourceList)

        })
    })

}